﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IIS
{
    public partial class MainForm : Form
    {
        private int port;
        private string virtualPath;
        private string physicalPath;
        private Server server;
        private bool Exit;

        public int Port
        {
            get
            {
                return this.port;
            }
            set
            {
                this.port = value;
            }
        }

        public string VirtualPath
        {
            get
            {
                return this.virtualPath;
            }
            set
            {
                this.virtualPath = value;
            }
        }

        public string PhysicalPath
        {
            get
            {
                return this.physicalPath;
            }
            set
            {
                this.physicalPath = value;
            }
        }

        /// <summary>
        /// 加载组件
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="msg"></param>
        private void WriteLog(string msg)
        {
            Action act = delegate ()
            {
                //txtLog.Text += msg + Environment.NewLine;
            };
            Invoke(act);
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            new Thread(() => { WriteLog(Environment.NewLine + "窗体加载......"); }).Start();
            try
            {
                base.SizeChanged += new EventHandler(this.MainForm_SizeChanged);
                this.Port = 8080; // 默认端口8080
                this.virtualPath = "/";
                this.physicalPath = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\"));
                PortForm pf = new PortForm();
                if (DialogResult.OK == pf.ShowDialog(this))
                {
                    WriteLog("注册服务......");
                    this.server = new Server(this.Port, this.VirtualPath, this.PhysicalPath);
                    WriteLog("开启服务......");
                    this.server.Start();

                    this.lklUrl.Text = "http://localhost:" + this.port;
                    this.txtPort.Text = this.port.ToString();
                    this.txtVirtualPath.Text = this.virtualPath;
                    this.txtPhysicalPath.Text = this.physicalPath;
                    this.notifyIcon.Text = this.physicalPath;
                    base.Visible = false;
                    this.notifyIcon.Visible = true;
                    this.notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
                    this.notifyIcon.BalloonTipTitle = this.physicalPath;
                    this.notifyIcon.BalloonTipText = "http://localhost:" + this.Port;
                    this.notifyIcon.ShowBalloonTip(2000);
                }
                else
                {
                    MessageBox.Show("您没有确认操作，将退出", "退出", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.btnStopService_Click(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "请将该文件拷贝到网站的bin目录再运行", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.btnStopService_Click(sender, e);
            }
        }

        /// <summary>
        /// 重启服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRestartService_Click(object sender, EventArgs e)
        {
            WriteLog("关闭服务......");
            ThreadPool.QueueUserWorkItem(this.server._restartCallback);
            WriteLog("重启中......");
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.reStart));
            WriteLog("重启完成......");
        }

        private void reStart(object unused)
        {
            this.notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
            this.notifyIcon.BalloonTipTitle = this.physicalPath;
            this.notifyIcon.BalloonTipText = "http://localhost:" + this.Port;
            this.notifyIcon.ShowBalloonTip(2000);
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStopService_Click(object sender, EventArgs e)
        {
            WriteLog("停止服务......");
            this.Exit = true;
            Application.Exit();
        }

        /// <summary>
        /// 窗体大小改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_SizeChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 打开链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lklUrl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            WriteLog("打开链接......");
            Process.Start(this.lklUrl.Text);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x11)
            {
                this.Exit = true;
            }
            base.WndProc(ref m);
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            WriteLog("窗体关闭......");
            if (!this.Exit)
            {
                e.Cancel = true;
                base.WindowState = FormWindowState.Minimized;
                base.Hide();
                this.notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
                this.notifyIcon.BalloonTipTitle = this.physicalPath;
                this.notifyIcon.BalloonTipText = "http://localhost:" + this.Port;
                this.notifyIcon.ShowBalloonTip(2000);
                return;
            }
            base.OnClosing(e);
            if (!e.Cancel)
            {
                this.notifyIcon.Dispose();
            }
        }

        /// <summary>
        /// 状态栏图标双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            base.Visible = true;
            base.WindowState = FormWindowState.Normal;
            this.notifyIcon.Visible = false;
        }

        /// <summary>
        /// 在浏览器打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiOpenInWeb_Click(object sender, EventArgs e)
        {
            WriteLog("在浏览器打开......");
            this.lklUrl_LinkClicked(sender, null);
        }

        /// <summary>
        /// 重启服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiRestartService_Click(object sender, EventArgs e)
        {
            WriteLog("重启服务......");
            btnRestartService_Click(sender, null);
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiStopService_Click(object sender, EventArgs e)
        {
            WriteLog("停止服务......");
            this.btnStopService_Click(sender, e);
        }

        /// <summary>
        /// 显示详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiShowDetailInfo_Click(object sender, EventArgs e)
        {
            WriteLog("显示详细信息......");
            this.notifyIcon_MouseDoubleClick(sender, null);
        }
    }
}
